#!/usr/bin/env python
#  winping.py uses your system's ping utility to send an ICMP ECHO_REQUEST 
#  to a list of hosts or devices.  This is useful for measuring network
#  latency and verifying hosts are alive
import sys
import re
from subprocess import Popen, PIPE
import os
import socket
#########################################################################                                           #
#                                                                       #
# Copyright (C) 2010 LeXeL                                              #
#                                                                       #
# This program is free software: you can redistribute it and/or modify  #
# it under the terms of the GNU General Public License as published by  #
# the Free Software Foundation, either version 3 of the License, or     #
# any later version.                                                    #
#                                                                       #
# This program is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################
def main():
    argc = len(sys.argv)
    if argc == 3:
        if '-h' in sys.argv:
            try:
                host = sys.argv[sys.argv.index('-h')+1]
                run(host)
            except:
                print "There was an Error with the commands\n -f: Expecify a file with a list of hosts\n -h: Ping a single host"
                sys.exit(1)
        if '-f' in sys.argv:
            try:
                file = sys.argv[sys.argv.index('-f')+1]
                f = open(file, "r")
                host = f.readlines()
                for h in host:
                    os.system("ping -n 1 " + h)
                    print "\n"
            except:
                print "There was an Error with the commands\n -f: Expecify a file with a list of hosts\n -h: Ping a single host"
                sys.exit(1)
        
    else:
        print "There was an Error with the commands\n -f: Expecify a file with a list of hosts\n -h: Ping a single host"
        sys.exit(1)

        
def run(host):
    try:
        ip = socket.gethostbyname(host)
        print "\t\nScanning %s with ip addres [%s]\n\n" %(host,ip)
        p = Popen('ping -n 1 ' + str(host), stdout=PIPE)
        m = re.search('Average = (.*)ms', p.stdout.read())# will work only if you have set to english language
        
    except:
        print "There was an Error with the ping please try again"
        
    if m: 
        print 'Round Trip Time: %s ms -' % m.group(1), host
    else: 
        print 'Error: Invalid Response -', host
if __name__ == '__main__':
    main()
